package cn.edu.xhu.library_management.department.service.impl;

import cn.edu.xhu.library_management.common.entity.ResultCode;
import cn.edu.xhu.library_management.common.exception.LibraryManagementException;
import cn.edu.xhu.library_management.department.entity.LmFaculty;
import cn.edu.xhu.library_management.department.entity.LmMajor;
import cn.edu.xhu.library_management.department.entity.search.SearchFaculty;
import cn.edu.xhu.library_management.department.mapper.FacultyMapper;
import cn.edu.xhu.library_management.department.mapper.MajorMapper;
import cn.edu.xhu.library_management.department.service.FacultyService;
import cn.edu.xhu.library_management.department.service.MajorService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: MMJ
 * @Date: 2022-01-28
 * @Description: 院系接口实现类
 */
@Service
public class FacultyServiceImpl implements FacultyService {
    @Resource
    private FacultyMapper facultyMapper;
    @Resource
    private MajorMapper majorMapper;

    @Override
    public boolean addFaculty(LmFaculty faculty) {
        // 去除名称中的首尾空格
        faculty.setName(faculty.getName().trim());
        // 查找院系名称是否存在
        LmFaculty faculty1 = facultyMapper.findFacultyByName(faculty.getName());
        if (faculty1 != null)
            throw new LibraryManagementException(ResultCode.OPERATION_ERROR, "院系已存在");
        return facultyMapper.addFaculty(faculty) > 0;
    }

    @Override
    public boolean addFaculties(List<LmFaculty> faculties) {
        return facultyMapper.addFaculties(faculties) > 0;
    }

    @Override
    public boolean updateFaculty(LmFaculty faculty) {
        // 查找院系名称是否存在
        LmFaculty faculty1 = facultyMapper.findFacultyByName(faculty.getName());
        if (faculty1 != null)
            throw new LibraryManagementException(ResultCode.OPERATION_ERROR, "院系已存在");
        return facultyMapper.updateFaculty(faculty) > 0;
    }

    @Override
    public boolean deleteFacultyById(Integer faId) {
        // 通过院系iD，查询专业是否清空
        List<LmMajor> majors = majorMapper.findMajorsByDepartment(faId);
        if (!majors.isEmpty())
            throw new LibraryManagementException(ResultCode.OPERATION_ERROR,"请先删除院系对应的专业");
        return facultyMapper.deleteFacultyById(faId) > 0;
    }

    @Override
    public boolean deleteFacultiesByIds(List<Integer> faIds) {
        // 通过院系iD，查询专业是否清空
        for (Integer faId : faIds) {
            List<LmMajor> majors = majorMapper.findMajorsByDepartment(faId);
            if (!majors.isEmpty()){
                throw new LibraryManagementException(ResultCode.OPERATION_ERROR,"有院系的专业为清空");
            }
        }

        return facultyMapper.deleteFacultiesByIds(faIds)>0;
    }

    @Override
    public List<LmFaculty> findAllFaculties() {
        return facultyMapper.findAllFaculties();
    }

    @Override
    public LmFaculty findFacultyById(Integer faId) {
        return facultyMapper.findFacultyById(faId);
    }

    @Override
    public Map<String,Object> search(SearchFaculty searchFaculty) {
        if (searchFaculty.getCurrentPage() != null && searchFaculty.getPageSize() != null){
            searchFaculty.setCurrentPage((searchFaculty.getCurrentPage() -1)*searchFaculty.getPageSize());
        }
        List<LmFaculty> faculties = facultyMapper.search(searchFaculty);
        // 获取总条数
        Integer total = facultyMapper.searchTotal(searchFaculty);
        Map<String, Object> map = new HashMap<>();
        map.put("faculties",faculties);
        map.put("total",total);
        return map;
    }
}
